package com.shuo.dsa.niuke;

import com.shuo.dsa.model.TreeNode;
import com.shuo.dsa.test.TreeUtil;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by shuoGG on 2018/10/31 0031
 */
public class TreeDepth {
    public int TreeDepth(TreeNode root) {
        if (root == null) return 0;
        return Math.max(TreeDepth(root.left), TreeDepth(root.right)) + 1;
    }

    public int TreeDepthBfs(TreeNode root) {
        if (root == null) return 0;
        Queue<TreeNode> queue = new LinkedList<>();
        int depth = 0;
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            depth++;
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                if (node.left != null)
                    queue.offer(node.left);
                if (node.right != null)
                    queue.offer(node.right);
            }
        }
        return depth;
    }

    public static void main(String[] args) {
        TreeNode treeNode = TreeUtil.createTree(20);
        TreeUtil.bfs(treeNode);
        System.out.println(new TreeDepth().TreeDepthBfs(treeNode));
    }

}
